package com.ruoyi.web.controller.statistics;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.statistics.domain.StatisticsWithdrawalExport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.statistics.domain.StatisticsWithdrawal;
import com.ruoyi.statistics.service.IStatisticsWithdrawalService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 *  信息操作处理
 * 
 * @author ruoyi
 * @date 2019-05-07
 */
@Controller
@RequestMapping("/statistics/statisticsWithdrawal")
public class StatisticsWithdrawalController extends BaseController
{
    private String prefix = "statistics/statisticsWithdrawal";
	
	@Autowired
	private IStatisticsWithdrawalService statisticsWithdrawalService;
	
	@RequiresPermissions("statistics:statisticsWithdrawal:view")
	@GetMapping()
	public String statisticsWithdrawal()
	{
	    return prefix + "/statisticsWithdrawal";
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("statistics:statisticsWithdrawal:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(StatisticsWithdrawal statisticsWithdrawal)
	{
		startPage();
        List<StatisticsWithdrawal> list = statisticsWithdrawalService.selectStatisticsWithdrawalList(statisticsWithdrawal);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出列表
	 */
	@RequiresPermissions("statistics:statisticsWithdrawal:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StatisticsWithdrawal statisticsWithdrawal)
    {
    	List<StatisticsWithdrawal> list = statisticsWithdrawalService.selectStatisticsWithdrawalList(statisticsWithdrawal);
		List<StatisticsWithdrawalExport> withdrawalExportList = dataConvert(list);
		ExcelUtil<StatisticsWithdrawalExport> util = new ExcelUtil<StatisticsWithdrawalExport>(StatisticsWithdrawalExport.class);
        return util.exportExcel(withdrawalExportList, "statisticsWithdrawal");
    }



	/**
	 * 数据转换
	 */
	public List<StatisticsWithdrawalExport> dataConvert(List<StatisticsWithdrawal> list) {
		List<StatisticsWithdrawalExport> withdrawalExportList = new ArrayList<>();
		for (StatisticsWithdrawal statisticsWithdrawal : list) {
			StatisticsWithdrawalExport statisticsWithdrawalExport = new StatisticsWithdrawalExport();
			BeanUtils.copyProperties(statisticsWithdrawal, statisticsWithdrawalExport);
			BigDecimal yuan = new BigDecimal("100");
			// 预期代付额
			statisticsWithdrawalExport.setExpectFee(statisticsWithdrawal.getExpectFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 预期渠道出账
			statisticsWithdrawalExport.setExpectChFee(statisticsWithdrawal.getExpectChFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 预期商户出账
			statisticsWithdrawalExport.setExpectMchFee(statisticsWithdrawal.getExpectMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 预期财务出账
			statisticsWithdrawalExport.setExpectFinFee(statisticsWithdrawal.getExpectFinFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际代付额
			statisticsWithdrawalExport.setRealFee(statisticsWithdrawal.getRealFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际渠道出账
			statisticsWithdrawalExport.setRealChFee(statisticsWithdrawal.getRealChFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际商户出账
			statisticsWithdrawalExport.setRealMchFee(statisticsWithdrawal.getRealMchFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));
			// 实际财务出账
			statisticsWithdrawalExport.setRealFinFee(statisticsWithdrawal.getRealFinFee().divide(yuan,2, BigDecimal.ROUND_HALF_UP));

			// 统计类型
			if (statisticsWithdrawal.getStatisticsType() == 1) {
				statisticsWithdrawalExport.setType("天");
			}else if (statisticsWithdrawal.getStatisticsType() == 2) {
				statisticsWithdrawalExport.setType("小时");
			}

			withdrawalExportList.add(statisticsWithdrawalExport);
		}
		return withdrawalExportList;
	}





	/**
	 * 新增
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存
	 */
	@RequiresPermissions("statistics:statisticsWithdrawal:add")
	@Log(title = "", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(StatisticsWithdrawal statisticsWithdrawal)
	{		
		return toAjax(statisticsWithdrawalService.insertStatisticsWithdrawal(statisticsWithdrawal));
	}

	/**
	 * 修改
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") Long id, ModelMap mmap)
	{
		StatisticsWithdrawal statisticsWithdrawal = statisticsWithdrawalService.selectStatisticsWithdrawalById(id);
		mmap.put("statisticsWithdrawal", statisticsWithdrawal);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存
	 */
	@RequiresPermissions("statistics:statisticsWithdrawal:edit")
	@Log(title = "", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(StatisticsWithdrawal statisticsWithdrawal)
	{		
		return toAjax(statisticsWithdrawalService.updateStatisticsWithdrawal(statisticsWithdrawal));
	}
	
	/**
	 * 删除
	 */
	@RequiresPermissions("statistics:statisticsWithdrawal:remove")
	@Log(title = "", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(statisticsWithdrawalService.deleteStatisticsWithdrawalByIds(ids));
	}
	
}
